package samfeng.erp.product.category.controller.admin;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import samfeng.erp.product.category.controller.admin.vo.*;
import samfeng.erp.product.category.convert.ProductCategoryConvert;
import samfeng.erp.product.category.dal.dataobject.ProductCategoryDO;
import samfeng.erp.product.category.service.ProductCategoryService;

@Api(tags = "管理后台 - 产品类别")
@RestController
@RequestMapping("/product/productCategory")
@Validated
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    @PostMapping("/create")
    @ApiOperation("创建产品类别")
    @PreAuthorize("@ss.hasPermission('product:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
        return success(productCategoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新产品类别")
    @PreAuthorize("@ss.hasPermission('product:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody ProductCategoryDO updateReqVO) {
        productCategoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除产品类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        productCategoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得产品类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<ProductCategoryRespVO> getCategoryDetail(@RequestParam("id") Long id) {
        ProductCategoryDO category = productCategoryService.getCategory(id);
        return success(ProductCategoryConvert.INSTANCE.convert(category));
    }

    @GetMapping("/list")
    @ApiOperation("获得产品类别列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<List<ProductCategoryRespVO>> getCategoryList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductCategoryDO> list = productCategoryService.getCategoryList(ids);
        return success(ProductCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/categoryTree")
    @ApiOperation("获得产品分类列表")
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<List<CategoryTreeListReqVO>> categoryTree() {
        List<CategoryTreeListReqVO> list = productCategoryService.getCategoryTreeList();
        return success(list);
    }

    @GetMapping("/page")
    @ApiOperation("获得产品类别分页")
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<PageResult<ProductCategoryRespVO>> getCategoryPage(@Valid ProductCategoryPageReqVO pageVO) {
        PageResult<ProductCategoryDO> pageResult = productCategoryService.getCategoryPage(pageVO);
        return success(ProductCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出产品类别 Excel")
    @PreAuthorize("@ss.hasPermission('product:category:export')")
    @OperateLog(type = EXPORT)
    public void exportCategoryExcel(@Valid ProductCategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductCategoryDO> list = productCategoryService.getCategoryList(exportReqVO);
        // 导出 Excel
        List<ProductCategoryExcelVO> datas = ProductCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品类别.xls", "数据", ProductCategoryExcelVO.class, datas);
    }

    /**
     * 上下架操作
     * @param status
     * @param id
     * @return AjaxResult
     */
    @ApiOperation(value = "上下架操作")
    @PutMapping("/updateStatus")
    @PreAuthorize("@ss.hasPermission('product:category:update')")
    public CommonResult<Boolean> updateStatusById(@RequestParam(value = "status") String status,
                                                  @RequestParam(value = "id") String id){
        productCategoryService.updateProuctStatus(status, id);
        return success(true);
    }

}
